/**
 * 
 */
package com.ssm.controller.admin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.ssm.controller.SSMController;
import com.ssm.exception.ExceptionCode;
import com.ssm.exception.SsmException;
import com.ssm.model.bean.SsmResources;
import com.ssm.model.mapper.ActionMapper;
import com.ssm.model.mapper.ResourceTypeMapper;
import com.ssm.model.mapper.ResourcesMapper;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * 
 * @author 危文涛<245108903@qq.com>
 * @version 2016年2月15日 下午5:54:09
 *
 */
@Controller
@RequestMapping(value="file")
public class FileController extends AdminBaseController<SsmResources>{
	
	@RequestMapping(value="index")
	public String index(HttpServletRequest request)
	{
		ResourcesMapper resourcesMapper=M.getMapper(ResourcesMapper.class);
		Example example=new Example(SsmResources.class);
		Criteria criteria= example.createCriteria();
		criteria.andCondition("userId=",this.getUserID(request));
		criteria.andCondition("status>","-1");
		request.setAttribute("LIST", resourcesMapper.selectByExample(example));
		request.setAttribute("WEB_TITLE","资源列表");
		return "file/index";
	}
	
	@RequestMapping(value="uploadFile")
	@ResponseBody
	public Map<String,Object> upload(HttpServletRequest request,SsmResources resources)
	{
		Map<String,Object> map=new HashMap<String,Object>();
		if(request.getMethod()=="POST")
		{
				if(UploadOneFile(request,resources)>0)
					return map;
		}
		throw new SsmException(ExceptionCode.PARAM_NOT_FIND);
	}
	
	
	@RequestMapping(value="edit")
	public String edit(HttpServletRequest request,SsmResources resources)
	{
		ResourcesMapper resourcesMapper=M.getMapper(ResourcesMapper.class);
		if(request.getMethod()=="POST")
		{
			if(resources.getId()!=null)
			{
				UploadOneFile(request,resources);
			}
			return "redirect:index" ;
		}
		else
		{
			Integer id=request.getParameter("id")==null?null:Integer.parseInt(request.getParameter("id"));
			ResourceTypeMapper resourceTypeMapper=M.getMapper(ResourceTypeMapper.class);
			request.setAttribute("typelist",resourceTypeMapper.selectAll());
			if(id==null)
			{
				request.setAttribute("WEB_TITLE","添加资源");
				return "file/upload";
			}
			else
			{
				resources=resourcesMapper.selectByPrimaryKey(id);
				if(resources==null)
					throw new SsmException(ExceptionCode.PARAM_NOT_FIND);
				else
					request.setAttribute("item",resources);
				return "file/edit";
			}
			
		}
	}
	
	@RequestMapping(value="changeStatus")
	public String changeStatus(HttpServletRequest request)
	{
		changeStatus(ResourcesMapper.class,request);
		return "redirect:index";
	}
}
